home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / C++ / Applications / nanoInstall 1.0 / source / writeThem.cp < prev   
Text File  |  1996-07-05  |  3KB  |  117 lines

  1. #include "nanoInstall.h"
  2.  
  3. OSErr writeOne( FSSpec &theFSSpec, short resNo);
  4.  
  5. OSErr PutForkAndClose( short theFork, OSType theType, short resNo);
  6.  
  7. OSErr writeThem()
  8. {
  9.     OSErr result = -1; // user canceled
  10.  
  11.     Handle promptResource = Get1Resource( 'STR ', 128);
  12.  
  13.     standardputfile sf( (unsigned char *)*promptResource);
  14.  
  15.     const short numFilesToInstall = Count1Resources( 'fNam');
  16.  
  17.     if( numFilesToInstall == 1)
  18.     {
  19.         Handle nameResource = Get1Resource( 'fNam', 128);
  20.  
  21.         if( sf.doIt( (unsigned char *)*nameResource))
  22.         {
  23.             result = writeOne( sf.sfFile, 128);
  24.         }
  25.         ReleaseResource( nameResource);
  26.     } else {
  27.         Handle folderNameResource = Get1Resource( 'STR ', 129);
  28.  
  29.         if( sf.doIt( (unsigned char *)*folderNameResource))
  30.         {
  31.             FSSpec theNewFSSpec;
  32.  
  33.             long createdDirID = 0;
  34.             result = FSpDirCreate( &sf.sfFile, smCurrentScript, &createdDirID);
  35.  
  36.             if( result == noErr)
  37.             {
  38.                 theNewFSSpec.vRefNum = sf.sfFile.vRefNum;
  39.                 theNewFSSpec.parID   = createdDirID;
  40.  
  41.                 short resNo = 128;
  42.  
  43.                 Handle nameResource = Get1Resource( 'fNam', resNo);
  44.  
  45.                 while( nameResource != 0)
  46.                 {
  47.                     BlockMoveData( *nameResource, theNewFSSpec.name, GetHandleSize( nameResource));
  48.  
  49.                     writeOne( theNewFSSpec, resNo);
  50.  
  51.                     resNo += 1;
  52.                     ReleaseResource( nameResource);
  53.                     nameResource = Get1Resource( 'fNam', resNo);
  54.                 }
  55.             }
  56.         }
  57.         ReleaseResource( folderNameResource);
  58.     }
  59.     ReleaseResource( promptResource);
  60.     return result;
  61. }
  62.  
  63. OSErr writeOne( FSSpec &theFSSpec, short resNo)
  64. {
  65.     OSErr result = noErr;
  66.  
  67.     (void)FSpDelete( &theFSSpec);
  68.     FSpCreateResFile( &theFSSpec, '????', '????', smCurrentScript);
  69.  
  70.     if( (result = ResError()) == noErr)
  71.     {
  72.         short resFork = 0;
  73.  
  74.         if( (result = FSpOpenRF( &theFSSpec, fsWrPerm, &resFork)) == noErr)
  75.         {
  76.             result = PutForkAndClose( resFork, 'RsFk', resNo);
  77.         }
  78.         short dataFork = 0;
  79.  
  80.         if( (result = FSpOpenDF( &theFSSpec, fsWrPerm, &dataFork)) == noErr)
  81.         {
  82.             result = PutForkAndClose( dataFork, 'DtFk', resNo);
  83.         }
  84.         //
  85.         // set finder info, except for the file location
  86.         //
  87.         FInfo fndrInfo;
  88.         (void)FSpGetFInfo( &theFSSpec, &fndrInfo);
  89.  
  90.         Handle theResource = Get1Resource( 'fInf', resNo);
  91.  
  92.         FInfo *origInfo        = (FInfo *)*theResource;
  93.         fndrInfo.fdType        = origInfo->fdType;
  94.         fndrInfo.fdCreator    = origInfo->fdCreator;
  95.         fndrInfo.fdFlags    = origInfo->fdFlags & (~kHasBeenInited);
  96.  
  97.         ReleaseResource( theResource);
  98.         (void)FSpSetFInfo( &theFSSpec, &fndrInfo);
  99.     }
  100.     return result;
  101. }
  102.  
  103. OSErr PutForkAndClose( short theFork, OSType theType, short resNo)
  104. {
  105.     Handle theResource = Get1Resource( theType, resNo);
  106.  
  107.     long numtowrite = GetHandleSize( theResource);
  108.  
  109.     OSErr result = FSWrite( theFork, &numtowrite, *theResource);
  110.  
  111.     FSClose( theFork);
  112.  
  113.     ReleaseResource( theResource);
  114.  
  115.     return result;
  116. }
  117.